package router

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"social-app/controllers"
	"social-app/middlewares"
)

func InitRouter(r *gin.Engine) *gin.Engine {
	defaultRouter(r)
	webRouter(r)
	return r
}

func webRouter(r *gin.Engine) {
	router := r.Group("/api")
	uploadRouter(router)
	commonRouter(router)
	authenticationRouter(router)
}

func defaultRouter(r *gin.Engine) {
	r.GET("/", controllers.DefaultController{}.Index)
	r.NoRoute(controllers.DefaultController{}.Handler)
}

func uploadRouter(r *gin.RouterGroup) {
	r.StaticFS("/uploads", http.Dir("./uploads"))
	upload := r.Group("/upload")
	upload.PUT("image", controllers.FileController{}.UploadImage)
	upload.PUT("video", controllers.FileController{}.UploadVideo)
	upload.PUT("music", controllers.FileController{}.UploadMusic)
}

func commonRouter(r *gin.RouterGroup) {
	dynamicCommon := r.Group("/dynamic")
	dynamicCommon.GET("/category", controllers.DynamicCategoryController{}.GetList)
	dynamicCommon.GET("", controllers.DynamicController{}.List)
	dynamicCommon.GET("/:id", controllers.DynamicController{}.Get)
	dynamicCommon.GET("/:id/reply", controllers.DynamicReplyController{}.GetList)

	userCommon := r.Group("/user")
	userCommon.POST("/login", controllers.UserController{}.Login)
	userCommon.POST("/register", controllers.UserController{}.Register)
	userCommon.POST("/wechat/login", (&controllers.WeChatController{}).Login)
}

func authenticationRouter(r *gin.RouterGroup) {
	// 下面是需要登录后才能进行访问的结构
	Authentication := r.Group("")
	Authentication.Use(middlewares.Authentication())

	user := Authentication.Group("/user")
	user.GET("/profile", controllers.UserController{}.GetUserInfo)
	user.POST("/profile", controllers.UserController{}.SetUserProfile)

	dynamic := Authentication.Group("/dynamic")
	dynamic.POST("", controllers.DynamicController{}.Create)
	dynamic.POST("/:id/reply", controllers.DynamicReplyController{}.Reply)
	dynamic.POST("/:id/praise", controllers.DynamicController{}.Praise)
}
